# Author: Stephen Situ
# MongoDB is a popular document based NoSQL database that is known for its scalability and flexible schema. The schema of
# MongoDB does not have to be specified and you can have fields/values that only apply to some documents in a collection.
# It also has replication and high availability built in. In this project, we use the pymongo library in Python to connect to
# a MongoDB database and practice CRUD operations in the database.
# importing MangoClient
from pymongo import MongoClient
# setting cluster
cluster = MongoClient("mongodb+srv://Joe:1234@cluster0.lz7wlnd.mongodb.net/test?retryWrites=true&w=majority")
# setting database
db = cluster["test"]
# setting collection
collection = db["todos"]
# Use python dictionary as a document, if no value given for _id, random object ID is given instead
post = {"_id":0, "name": "tim", "score":5}
# To add document use insert_one and insert_many
collection.insert_one(post)
results = collection.find({})
for result in results:
print(result)
{'_id': 0, 'name': 'tim', 'score': 5}
# We Can define multiple posts
post1 = {"_id":5, "name":"joe"}
post2 = {"_id":6, "name":"bill"}
# Use insert_many
collection.insert_many([post1,post2])
results = collection.find({})
for result in results:
print(result)
{'_id': 0, 'name': 'tim', 'score': 5} {'_id': 5, 'name': 'joe'} {'_id': 6, 'name': 'bill'}
# Using find_one (only first result) and find (all results) query to filter
results = collection.find({"name":"bill"})
for result in results:
print(result)
{'_id': 6, 'name': 'bill'}
# if only want one field like "_id" in results
results = collection.find({"name":"bill"})
for result in results:
print(result["_id"])
6
# Looking for multiple fields (both need to match)
results = collection.find({"name":"tim","_id":0})
for result in results:
print(result)
{'_id': 0, 'name': 'tim', 'score': 5}
# Using find_one to only find one entry
results = collection.find_one({"name":"tim","_id":0})
print(results)
{'_id': 0, 'name': 'tim', 'score': 5}
# To return everything, use empty dictionary
results = collection.find({})
for result in results:
print(result)
{'_id': 0, 'name': 'tim', 'score': 5} {'_id': 5, 'name': 'joe'} {'_id': 6, 'name': 'bill'}
# Using delete_one & delete_many to delete document
collection.delete_one({"_id":0})
results = collection.find({})
for result in results:
print(result)
{'_id': 5, 'name': 'joe'} {'_id': 6, 'name': 'bill'}
# to update, use update_one & update_many with $set operator
collection.update_one({"_id":5,}, {"$set":{"name":"tim"}})
results = collection.find({})
for result in results:
print(result)
{'_id': 5, 'name': 'tim'} {'_id': 6, 'name': 'bill'}
# to add a new field to a document, simply use $set and add the new field
collection.update_one({"_id":5,}, {"$set":{"pet":"cat"}})
results = collection.find({})
for result in results:
print(result)
{'_id': 5, 'name': 'tim', 'pet': 'cat'} {'_id': 6, 'name': 'bill'}
# can use replace_one and replace_many to replace all entries in document
collection.replace_one({"_id":6},{"identification":60})
results = collection.find({})
for result in results:
print(result)
{'_id': 5, 'name': 'tim', 'pet': 'cat'} {'_id': 6, 'identification': 60}
# Can use aggragate functions like count
# Count all
post_count = collection.count_documents({})
print(post_count)
2
# specific criteria
post_count = collection.count_documents({"_id":5})
print(post_count)
1